package com.ruoyi.web.controller.job;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.JobPlanBillDeclare;
import com.ruoyi.system.domain.JobPlanBillDeclareRenewal;
import com.ruoyi.system.domain.bill.JobPersonnel;
import com.ruoyi.system.domain.vo.DeleteVo;
import com.ruoyi.system.service.JobPlanBillDeclareService;
import com.ruoyi.system.services.PersonPositionServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作业计划 - 申报(JobPlanBillDeclare)表控制层
 *
 * @since 2022-11-23 09:20:09
 */
@Slf4j
@RestController
@Api(tags = "作业计划-申报")
@RequestMapping("job/plan/bill/declare")
public class JobPlanBillDeclareController extends BaseController {

    @Resource
    private JobPlanBillDeclareService jobPlanBillDeclareService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("list")
    @ApiOperation("获取列表")
    public TableDataInfo queryByPage(JobPlanBillDeclare jobPlanBillDeclare) {
        startPage();
        List<JobPlanBillDeclare> list = jobPlanBillDeclareService.queryAll(jobPlanBillDeclare);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("获取详情")
    public ResponseEntity<JobPlanBillDeclare> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.jobPlanBillDeclareService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @PostMapping("add")
    @ApiOperation("新增")
    public AjaxResult add(@Validated @RequestBody JobPlanBillDeclare jobPlanBillDeclare) {
      /*  String checkParam = checkParam(jobPlanBillDeclare);
        if (null != checkParam) {
            return AjaxResult.error(checkParam);
        }*/
        JobPlanBillDeclare insert = this.jobPlanBillDeclareService.insert(jobPlanBillDeclare);
        return AjaxResult.success(insert);
    }

    /**
     * 编辑数据
     *
     * @return 编辑结果
     */
    @PutMapping("edit")
    @ApiOperation("修改")
    public AjaxResult edit(@Validated @RequestBody JobPlanBillDeclare jobPlanBillDeclare) {
      /*  String checkParam = checkParam(jobPlanBillDeclare);
        if (null != checkParam) {
            return AjaxResult.error(checkParam);
        }*/
        JobPlanBillDeclare update = this.jobPlanBillDeclareService.update(jobPlanBillDeclare);
        return AjaxResult.success(update);
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    @ApiOperation("删除")
    public AjaxResult deleteById(@Validated @RequestBody DeleteVo deleteVo) {
    /*    //账号权限校验;
        String username = SecurityUtils.getLoginUser().getUsername();
        if (!"admin".equals(username)) {
            return AjaxResult.error("无权限");
        }*/
        this.jobPlanBillDeclareService.deleteById(deleteVo.getId());
        return AjaxResult.success();
    }

    /**
     * 分页查询(人员信息)
     *
     * @return 查询结果
     */
    @GetMapping("queryJobPersonnel")
    @ApiOperation("获取列表")
    public TableDataInfo queryByPage(JobPersonnel jobPersonnel) {
        startPage();
        List<JobPersonnel> list = jobPlanBillDeclareService.queryJobPersonnel(jobPersonnel);
        return getDataTable(list);
    }


    /**
     * 分页查询（过滤结束时间超期的）
     *
     * @return 查询结果
     */
    @GetMapping("queryJobTimeoutList")
    @ApiOperation("获取列表")
    public TableDataInfo queryJobTimeoutList(JobPlanBillDeclare jobPlanBillDeclare) {
        startPage();
        List<JobPlanBillDeclare> list = jobPlanBillDeclareService.queryJobTimeout(jobPlanBillDeclare);
        return getDataTable(list);
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @PostMapping("renewal")
    @ApiOperation("续签")
    public AjaxResult renewal(@Validated @RequestBody JobPlanBillDeclareRenewal jobPlanBillDeclareRenewal) {
        if (null == jobPlanBillDeclareRenewal) {
            return AjaxResult.error("参数异常");
        }
        if (null == jobPlanBillDeclareRenewal.getId()) {
            return AjaxResult.error("id不能为空");
        }
        if (StringUtils.isEmpty(jobPlanBillDeclareRenewal.getJobPlanEndDate()) || StringUtils.isEmpty(jobPlanBillDeclareRenewal.getJobPlanEndTime())) {
            return AjaxResult.error("续签时间不能为空");
        }
        JobPlanBillDeclare jobPlanBillDeclare = jobPlanBillDeclareService.renewal(jobPlanBillDeclareRenewal);
        return AjaxResult.success(jobPlanBillDeclare);
    }

    private String checkParam(JobPlanBillDeclare jobPlanBillDeclare) {
        if (StringUtils.isEmpty(jobPlanBillDeclare.getApplyCompanyId())) {
            return "作业申请单位不可为空";
        }

        if (StringUtils.isEmpty(jobPlanBillDeclare.getJobPersonnel())) {
            return "作业人员不可为空";
        }

        if (StringUtils.isEmpty(jobPlanBillDeclare.getWorkShop())) {
            return "作业单位不可为空";
        }

        if (StringUtils.isEmpty(jobPlanBillDeclare.getApplyPeopleId())) {
            return "申请人不可为空";
        }

        if (StringUtils.isEmpty(jobPlanBillDeclare.getJobPersonChargeId())) {
            return "作业负责人不可为空";
        }

        if (StringUtils.isEmpty(jobPlanBillDeclare.getPhone())) {
            return "联系方式不可为空";
        }

        if (StringUtils.isEmpty(jobPlanBillDeclare.getDeclareType())) {
            return "申报类型不可为空";
        }

        return null;
    }

}

